require(tidyverse)
require(dplyr)
files = list.files("results")[grepl("lag", list.files("results"))]

results_df = as.data.frame(matrix(nrow = 0, ncol = 8))
colnames(results_df) = c("coef", "se", "race", "dv", "endog", "state", "specification", "lag")



for(ob in files){
  load(paste0("results/", ob))
  holder = as.data.frame(matrix(nrow = 21, ncol = 8))
  colnames(holder) = c("coef", "se", "race", "dv", "endog", "state", "specification", "lag")

  holder$coef = c(
              
                   
                   
                   ivmod1_white$cace, ivmod1_black$cace, ivmod1_hispanic$cace,
                    ivmod2_white$cace, ivmod2_black$cace, ivmod2_hispanic$cace,
                   ivmod3_white$cace, ivmod3_black$cace, ivmod3_hispanic$cace,
                   ivmod4_white$cace, ivmod4_black$cace, ivmod4_hispanic$cace,
                   ivmod5_white$cace, ivmod5_black$cace, ivmod5_hispanic$cace,
                   ivmod6_white$cace, ivmod6_black$cace, ivmod6_hispanic$cace,
                    ivmod_rep_white$cace, ivmod_rep_black$cace, ivmod_rep_hispanic$cace
                   
                   
                   
                   
                   )
  
  holder$se = c(
                 ivmod1_white$se, ivmod1_black$se, ivmod1_hispanic$se,
                  ivmod2_white$se, ivmod2_black$se, ivmod2_hispanic$se,
                 ivmod3_white$se, ivmod3_black$se, ivmod3_hispanic$se,
                 ivmod4_white$se, ivmod4_black$se, ivmod4_hispanic$se,
                 ivmod5_white$se, ivmod5_black$se, ivmod5_hispanic$se,
                 ivmod6_white$se, ivmod6_black$se, ivmod6_hispanic$se,
                 ivmod_rep_white$se, ivmod_rep_black$se, ivmod_rep_hispanic$se
                 
  )

 
  ##############
  holder$race = c(rep(c("white", "black", "hispanic"), 7))
  
  holder$dv = c(rep(c(rep("Vote 2016", 3),
                 rep("Vote 2014", 3),
                 rep("Vote 2012", 3),
                rep("Vote 2010", 3),
                 rep("Democrat", 3),
                 rep("Registered", 3),
                 rep("Republican", 3))
                 
                 ))
  
  
  holder$endog = "2008"
  
  holder$specification = str_extract(ob, "[[:digit:]]+")
  
  holder$lag = ifelse(grepl("nolag", ob), "nolag", "lag")
                    
  holder$state = substr(ob,9,10)
  
  
  results_df = rbind(results_df, holder)
  
  print(ob)
}



data = results_df

output = list()
for(sp in unique(data$specification)){
  holder = list()
  for(l in unique(data$lag)){
    
    block = data[data$lag ==l & data$specification == sp,]
    predictions_white = as.data.frame(matrix(NA, ncol = 7))
    predictions_black = as.data.frame(matrix(NA, ncol = 7))
    predictions_hispanic = as.data.frame(matrix(NA, ncol = 7))
    
    

    for(j in 1:length(unique(block$dv))){
      if(length(block$dv[block$dv == unique(block$dv)[j]]) > 0){
        
        preds_white = meta.maker(block$coef[block$race == "white"  & block$dv == unique(block$dv)[j] ], block$se[ block$race == "white" & block$dv == unique(block$dv)[j]])
        predictions_white = rbind(predictions_white, c(sp,l, unique(block$dv)[j], preds_white$est, preds_white$se, "white", "2008"))
        
        preds_black = meta.maker(block$coef[ block$race == "black" & block$dv == unique(block$dv)[j]], block$se[ block$race == "black"  & block$dv == unique(block$dv)[j]])
        predictions_black = rbind(predictions_black, c(sp,l, unique(block$dv)[j], preds_black$est, preds_black$se, "black", "2008"))
        
        preds_hispanic = meta.maker(block$coef[ block$race == "hispanic" &  block$dv == unique(block$dv)[j]], block$se[ block$race == "hispanic" & block$dv == unique(block$dv)[j]])
        predictions_hispanic = rbind(predictions_hispanic, c(sp,l, unique(block$dv)[j], preds_hispanic$est, preds_hispanic$se, "hispanic", "2008"))
        
        
        
        
        
      }
      
    }
    
    meta_plot=rbind(predictions_white, predictions_black, predictions_hispanic )
    colnames(meta_plot)=c('specification', 'lag', 'dv', 'coef', 'se', 'race', 'ev' )
    holder[[l]]=meta_plot
    rm(meta_plot,predictions_white,predictions_black,predictions_hispanic, block)
  }
  output[[paste0("spec",sp)]]=holder
}



df = rbind(output$spec0$lag, 
           output$spec0$nolag,
           output$spec2$lag, 
           output$spec2$nolag,
           output$spec3$lag, 
           output$spec3$nolag,
           output$spec1$nolag)

df = df[!is.na(df$specification),]
df$coef = as.numeric(df$coef)
df$se = as.numeric(df$se)


out = data.frame(file = c('alt_results_lag_spec0.pdf', 'alt_results_lag_spec2.pdf', 'alt_results_lag_spec3.pdf',
                          'alt_results_nolag_spec1.pdf', 'alt_results_nolag_spec0.pdf', 'alt_results_nolag_spec2.pdf', 'alt_results_nolag_spec3.pdf'),
                 out.path = paste0('figures/FigS',13:19,'.pdf'))

for(sp in unique(df$specification)){
  for(l in unique(df$lag)){
    if(!(l=='lag' & sp =='1')){
    plt = df[  df$lag == l & df$specification == sp,]
    p=ggplot(data = plt[plt$dv %in% c("Vote 2010","Vote 2012", "Vote 2014", "Vote 2016"),]%>%
               mutate(race=Hmisc::capitalize(race))%>%
               mutate(race=factor(race,levels = c('Black','White','Hispanic'))), aes(x = dv, y = coef, color = race, fill = race)) +
      geom_hline(yintercept = 0, linetype = 'dotted', color = 'grey60') +
      geom_point(shape = 21, size = 6, position=position_dodge(.5)) + xlab('') +
      ylab('Effect of 2008 Voting on Future Voting') + 
      geom_errorbar(aes(ymin=coef-1.96*se, ymax=coef+1.96*se),linetype = "solid", width=0,
                    position=position_dodge(.5)) +
      scale_color_manual(values = c("black", "black", "black"))+
      scale_fill_manual(values = c("black", "white", "grey"))+
      guides(linetype=F)+
      theme_minimal()+
      scale_x_discrete(labels = c('2010', '2012',"2014", "2016"))+
      theme(axis.text.y=element_text(face='bold', size =14), axis.title.y=element_text(face='bold', size =14), 
            axis.text.x = element_text(face="bold", size =14), legend.text=element_text(face='bold', size =14),
            legend.position="bottom", legend.spacing.x = unit(.25, 'cm'),legend.title = element_blank(), legend.key.width=unit(4,"line"))+
      NULL
    f= paste0("alt_results_",l,"_spec",sp,".pdf")
    ggsave(plot = p,filename = out$out.path[out$file==f], dpi = 300, width = 9, height = 6, units = 'in')
    
  }
}
}
